package com.bdqn.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.bus.entity.Customer;
import com.bdqn.bus.service.CustomerService;
import com.bdqn.bus.vo.CustomerVo;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/bus/customer")
public class CustomerController {


    @Resource
    private CustomerService customerService;

    @RequestMapping("/list")
    public DateGindViewResult list(CustomerVo customerVo){
        //分页对象
        IPage<Customer> iPage=new Page<Customer>(customerVo.getPage(),customerVo.getLimit());
        //条件构造器
        QueryWrapper<Customer> queryWrapper=new QueryWrapper<Customer>();
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getCustname()),"custname",customerVo.getCustname());//客户姓名
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getIdentity()),"identity",customerVo.getIdentity());//身份号
        queryWrapper.ge(customerVo.getStartTime()!=null,"createtime",customerVo.getStartTime());
        queryWrapper.le(customerVo.getEndTime()!=null,"createtime",customerVo.getEndTime());
        //调用查询的方法
        customerService.page(iPage,queryWrapper);
        //返回数据
        return new DateGindViewResult(iPage.getTotal(),iPage.getRecords());
    }


    /**
     * 客户添加
     */
    @RequestMapping("/addCustomer")
    public JSONResult addCustomer(Customer customer){
        customer.setCreatetime(new Date());//创建时间
        if(customerService.save(customer)){
            return SystemConstants.Add_SUCCESS;
        }
        return SystemConstants.Add_ERROR;
    }


    /**
     * 客户修改
     */
    @RequestMapping("/updateCustomer")
    public JSONResult updateCustomer(Customer customer){
        customer.setCreatetime(new Date());//创建时间
        if(customerService.updateById(customer)){
            return SystemConstants.UPDATE_SUCCESS;
        }
        return SystemConstants.UPDATE_ERROR;
    }


    /**
     * 客户删除
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id){
        if(customerService.removeById(id)){
            return SystemConstants.DELETE_SUCCESS;
        }
        return SystemConstants.DELETE_ERROR;
    }
}

